Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import { eq } from 'drizzle-orm' import { NextResponse } from 'next/server' import { withAuth } from '@/lib/auth/withAuth' import { db, schema } from '@/db' import { enrollmentRequests } from '@/db/schema' import { approveEnrollmentRequest, isParentOf } from '@/lib/classroom' import { emitEnrollmentCompleted, emitEnrollmentRequestApproved, } from '@/lib/classroom/socket-emitter' import { getUserId } from '@/lib/viewer' /** * POST /api/enrollment-requests/[requestId]/approve * Parent approves enrollment request * * Returns: { request, enrolled: boolean } */ export const POST = withAuth(async (_request, { params }) => { try { const { requestId } = (await params) as { requestId: string } const userId = await getUserId() // Get the request to verify parent owns the child const request = await db.query.enrollmentRequests.findFirst({ where: eq(enrollmentRequests.id, requestId), }) if (!request) { return NextResponse.json({ error: 'Request not found' }, { status: 404 }) } // Verify user is a parent of the child in the request const parentCheck = await isParentOf(userId, request.playerId) if (!parentCheck) { return NextResponse.json({ error: 'Not authorized' }, { status: 403 }) } const result = await approveEnrollmentRequest(requestId, userId, 'parent') // Emit socket events for real-time updates try { const classroomId = result.request.classroomId // Get classroom and player info for socket events const [classroomInfo] = await db .select({ name: schema.classrooms.name }) .from(schema.classrooms) .where(eq(schema.classrooms.id, classroomId)) .limit(1) const [playerInfo] = await db .select({ name: schema.players.name }) .from(schema.players) .where(eq(schema.players.id, result.request.playerId)) .limit(1) if (classroomInfo && playerInfo) { const payload = { requestId, classroomId, classroomName: classroomInfo.name, playerId: result.request.playerId, playerName: playerInfo.name, } if (result.fullyApproved) { // Both sides approved - notify teacher and student await emitEnrollmentCompleted(payload, { classroomId, // Teacher sees the update playerIds: [result.request.playerId], // Student's enrolled classrooms list updates }) } else { // Only parent approved - notify teacher that parent approved their request await emitEnrollmentRequestApproved({ ...payload, approvedBy: 'parent' }, { classroomId }) } } } catch (socketError) { console.error('[Parent Approve] Failed to emit socket event:', socketError) } return NextResponse.json({ request: result.request, enrolled: result.fullyApproved, }) } catch (error) { console.error('Failed to approve enrollment request:', error) const message = error instanceof Error ? error.message : 'Failed to approve enrollment request' return NextResponse.json({ error: message }, { status: 500 }) } }) |